package com.sxsihe.heartmate;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import cn.pedant.SweetAlert.widget.SweetAlertDialog;
import com.sxsihe.heartmate.util.HttpPostUtil;
import com.sxsihe.heartmate.util.SharePreferenceUtil;
import com.sxsihe.heartmate.view.CustomWaittingDialog;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

public class UserRegisterActivity extends Activity {
    Button btn_register;
    Button btn_retrieve_password;
    EditText phone;
    EditText password;
    CustomWaittingDialog dialog;
    SharePreferenceUtil spu;

    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        setContentView(R.layout.user_register);
        if (spu == null) {
            spu = SharePreferenceUtil.getInstance(this);
        }
        dialog = new CustomWaittingDialog(this,R.anim.connect_waitting);
        phone = (EditText)findViewById(R.id.phone);
        password = (EditText)findViewById(R.id.password);
        btn_register = (Button)findViewById(R.id.btn_register);
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                String url = "http://"+spu.get("server_ip","")+":"+spu.get("server_port","")+"/zhcjhy/loginAction.do?action=mobileRegist";
                String url = "http://"+spu.get("server_ip","");
                if(spu.get("server_port","").equals("")){
                    url +=":"+spu.get("server_port","");
                }
                url +="/zhcjhy/loginAction.do?action=mobileRegist";
                dialog.showWaitting();
                String args [] = {url,phone.getText().toString(),password.getText().toString()};
                HttpPostUtil post = new HttpPostUtil(UserRegisterActivity.this,"register");
                post.execute(args);
            }
        });
        btn_retrieve_password = (Button)findViewById(R.id.btn_retrieve_password);
        btn_retrieve_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UserRegisterActivity.this,GetBackPwdActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    public void registerResult(String result){
        dialog.hideWaitting();
        if ("".equals(result)){
            new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
                    .setTitleText("出错了")
                    .setContentText("通讯异常")
                    .show();
        }else{
            try {
                JSONObject json = new JSONObject(result);
                if(json.getBoolean("success")){
                    new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE)
                            .setTitleText("恭喜")
                            .setContentText("注册成功")
                            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    Intent intent = new Intent(UserRegisterActivity.this,MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            })
                            .show();
                }else{
                    new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
                            .setTitleText("出错了")
                            .setContentText(json.getString("msg"))
                            .show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
